/*
 * Copyright (c) 2016 MaxSky.TK. All Rights Reserved.
 */

package cn.studyjams.s1.sj47.maxsky;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
		implements NavigationView.OnNavigationItemSelectedListener {
	private ActionBarDrawerToggle mDrawerToggle;
	private Toolbar toolbar;
	private FragmentManager fragmentManager = getSupportFragmentManager();
	private DrawerLayout mDrawer;
	private NavigationView navigationView;
	private long mExitTime = 0;
	/**
	 * @fragmentIndex 用于判断在哪一个 Fragment
	 * 0 = Main
	 * 1 = Boys
	 * 2 = Girls
	 * 3 = Pupil
	 * 4 = Middle Student
	 * 5 = University Student
	 */
	private int fragmentIndex = 0;

	public ActionBarDrawerToggle getmDrawerToggle() {
		return mDrawerToggle;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		toolbar = (Toolbar) findViewById(R.id.toolbar);
		if (toolbar != null) {
			toolbar.setLogo(R.mipmap.ic_launcher);
		}
		setSupportActionBar(toolbar);

		fragmentManager.beginTransaction().replace(R.id.main_frame_content,
				new MainFragment()).commit();

		mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerToggle = new ActionBarDrawerToggle(
				this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
		if (mDrawer != null) {
			mDrawer.addDrawerListener(mDrawerToggle);   // setDrawerListener 过时，addDrawerListener 已代替
		}
		mDrawerToggle.syncState();

		navigationView = (NavigationView) findViewById(R.id.main_nav_view);
		if (navigationView != null) {
			navigationView.setNavigationItemSelectedListener(this);
		}

	}

//	@Override
//	public void onBackPressed() {
//		DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//		if (drawer.isDrawerOpen(GravityCompat.START)) {
//			drawer.closeDrawer(GravityCompat.START);
//		} else {
//			super.onBackPressed();
//		}
//	}

	public void reloadActivity(View v) {
		finish();
		fragmentIndex = 0;
		Intent intent = new Intent(MainActivity.this, MainActivity.class);
		startActivity(intent);
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			if (fragmentIndex == 0) {
				if ((System.currentTimeMillis() - mExitTime) > 2000) {
					Toast.makeText(MainActivity.this, "再次返回将退出程序", Toast.LENGTH_SHORT).show();
					mExitTime = System.currentTimeMillis();
				} else {
					finish();
					System.exit(0);
				}
			} else {
				fragmentIndex = 0;
				mDrawer.closeDrawer(navigationView);
				fragmentManager.beginTransaction().replace(R.id.main_frame_content,
						new MainFragment()).commit();
			}
		}
		return true;
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.

		getMenuInflater().inflate(R.menu.menu_launcher, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		//noinspection SimplifiableIfStatement

		if (item.getItemId() == R.id.action_about) {
			Intent intent = new Intent(MainActivity.this, AboutActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	@SuppressWarnings("StatementWithEmptyBody")
	@Override
	public boolean onNavigationItemSelected(MenuItem item) {

		// Handle navigation view item clicks here.

		int id = item.getItemId();
		switch (id) {
			case R.id.nav_camera:
				fragmentIndex = 1;
				toolbar.setTitle("我是一只男孩纸");
				mDrawer.closeDrawer(navigationView);
				fragmentManager.beginTransaction().replace(R.id.main_frame_content,
						new BoysFragment()).commit();
				break;
			case R.id.nav_gallery:
				fragmentIndex = 2;
				toolbar.setTitle("我是一只女孩纸");
				mDrawer.closeDrawer(navigationView);
				fragmentManager.beginTransaction().replace(R.id.main_frame_content,
						new GirlsFragment()).commit();
				break;
			case R.id.nav_slideshow:
				fragmentIndex = 3;
				toolbar.setTitle("小雪升表示不服");
				mDrawer.closeDrawer(navigationView);
				fragmentManager.beginTransaction().replace(R.id.main_frame_content,
						new PupilFragment()).commit();
				break;
			case R.id.nav_manage:
				fragmentIndex = 4;
				toolbar.setTitle("中雪升肿么了？");
				mDrawer.closeDrawer(navigationView);
				fragmentManager.beginTransaction().replace(R.id.main_frame_content,
						new MidStudentFragment()).commit();
				break;
			case R.id.nav_share:
				fragmentIndex = 5;
				toolbar.setTitle("苦逼大学单身狗就看看");
				mDrawer.closeDrawer(navigationView);
				fragmentManager.beginTransaction().replace(R.id.main_frame_content,
						new UniStudentFragment()).commit();
				break;
			default:
				if (mDrawer != null) {
					mDrawer.closeDrawer(GravityCompat.START);
				}
				break;
		}
		return true;
	}
}
